package com.example.module_hp_zao_yin_media.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.example.module_hp_zao_yin_media.dailog.BaseCommonDialog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DownBitmap {
    //保存图片
    public static Uri saveBitmap2File(Context mContext, Bitmap bmp, String folder, String mimeType) throws IOException {
        String format = ".jpg";
        String filePath = "Download";
        Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG;
        if (mimeType.contains("png")) {
            format = ".png";
            compressFormat = Bitmap.CompressFormat.PNG;
        }
        if(folder!=null){
            filePath= folder;
        }
        String fileName = System.currentTimeMillis() + format;
        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/"+filePath);
        if (!file.exists()) file.mkdir();
        String path = Environment.getExternalStorageDirectory().getPath() +"/"+filePath+"/" + fileName;
        OutputStream stream = new FileOutputStream(path);
        bmp.compress(compressFormat, 100, stream);
        stream.flush();
        stream.close();
        //把图片保存后声明这个广播事件通知系统相册有新图片到来
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(new File(path));
        intent.setData(uri);
        mContext.sendBroadcast(intent);

        // 兼容鸿蒙，防止相册不显示图片
        File newFile = new File(path);
        MediaScannerConnection.scanFile(mContext, new String[]{newFile.toString()}, new String[]{newFile.getName()}, null);

        //成功弹窗
        succeedDialog(mContext,filePath);
        return Uri.fromFile(new File(fileName));
    }
    //成功弹窗
    public static void succeedDialog(Context mContext, String filePath){
        final BaseCommonDialog succDialog = new BaseCommonDialog(mContext);
        succDialog.setTitle("保存成功");
        succDialog.setMessage("文件已保存到"+filePath+"文件夹，可从相册或文件管理进行查看")
                .setNegtive("取消")
                .setPositive("再做一个")
                .setOnClickBottomListener(new BaseCommonDialog.OnClickBottomListener() {
                    @Override
                    public void onPositiveClick() {
                        succDialog.dismiss();
                    }
                    @Override
                    public void onNegtiveClick() {
                        succDialog.dismiss();
                    }
                }).show();
        succDialog.getWindow().setLayout(getScreenWidth(mContext) - 200,
                WindowManager.LayoutParams.WRAP_CONTENT);//设置弹出框宽度为屏幕宽度的三分之二
    }
    public static int getScreenWidth(Context context){
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
//        int width = dm.widthPixels;         // 屏幕宽度（像素）
//        int height = dm.heightPixels;       // 屏幕高度（像素）
//        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
//        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
//        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
//        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
//        int screenHeight = (int) (height / density);// 屏幕高度(dp)
        return dm.widthPixels;
    }
}
